package com.terrynoya.fight.stateMachine.triggers
{
	import com.terrynoya.fight.animations.IAnimation;
	import com.terrynoya.fight.animations.IAnimationElement;
	import com.terrynoya.fight.char.Character;
	import com.terrynoya.fight.emuns.Operator;
	import com.terrynoya.fight.emuns.SymbolType;
	import com.terrynoya.fight.evaluation.Node;
	import com.terrynoya.fight.evaluation.ParseState;
	import com.terrynoya.fight.evaluation.SpecialFunctions;

	public class AnimElem
	{
		public function AnimElem()
		{
			
		}
		
		public static function eval(data:Object,node:Node):*
		{
			var chara:Character = Character(data);
			if(chara == null)
			{
				return false;
			}
			var ani:IAnimation = chara.animationManager.currentAni;
			if(ani == null)
			{
				return false;
			}
			
			var val:int = node.getChildAt(0).excute(data);
			var elementId:int = val - 1;
			
			if(elementId < 0 || ani.elementLength <= elementId)
			{
				return false;	
			}
			var aniTime:int  = chara.animationManager.time;
			var eleStartTime:int = ani.getElementStartTime(elementId);
			
			while(ani.totalTime != -1 && aniTime >= ani.totalTime)
			{
				var loopTime:int = ani.totalTime - ani.getElementStartTime(ani.loopStartId);
				aniTime -= loopTime;
			}
			var timeOffset:int = aniTime - eleStartTime;
			
			if(chara.animationManager.isAniFinished)
			{
				return false;
			}
			
			var rhs:int = node.getChildAt(1).excute(data);
			var oper:int = node.arguments[1];
			
			var rlt:Boolean = SpecialFunctions.compare(oper,timeOffset,rhs);
			return rlt;
//			var currEle:IAnimationElement = chara.animationManager.currentElement;
//			var oper:int = node.arguments[0];
//			switch(oper)
//			{
//				case Operator.Equals:
//				{
//					return timeOffset == 0;
//					break;
//				}
//				case Operator.NotEquals:
//				{
//					return timeOffset != 0;
//					break;
//				}
//			}
			return false;
		}
		
		public static function parse(state:ParseState):Node
		{
			var initOper:int = state.currentOperator;
			switch(initOper)
			{
				case Operator.Equals:
				case Operator.NotEquals:
				case Operator.GreaterEquals:
				case Operator.LesserEquals:
					state.tokenIndex ++;
					break;
				default:
				{
					return null;
				}
			}
			
			var arg1:Node = state.buildNode(false);
			if(arg1 == null)
			{
				return null;
			}
			state.baseNode.chilren.push(arg1);
			state.baseNode.arguments.push(initOper);
			
			if(state.currentSymbol != SymbolType.Comma)
			{
				state.baseNode.addChild(Node.zeroNode);
				state.baseNode.arguments.push(Operator.Equals);
				return state.baseNode;
			}
			
			state.tokenIndex ++;
			
			var oper:int = state.currentOperator;
			
			switch(oper)
			{
				case Operator.Equals:
				case Operator.NotEquals:
				case Operator.GreaterEquals:
				case Operator.LesserEquals:
				case Operator.Lesser:
				case Operator.Greater:
					state.tokenIndex ++;
					break;
				default:
					return null;
			}
			
			var arg:Node = state.buildNode(false);
			if(arg == null)
			{
				return null;
			}
			state.baseNode.arguments.push(oper);
			state.baseNode.chilren.push(arg);
			return state.baseNode;
		}
	}
}